package com.xyz.keshe.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xyz.keshe.db.DatabaseHelper;
import com.xyz.keshe.model.Comment;

import java.util.ArrayList;
import java.util.List;

public class CommentDao {
    private final DatabaseHelper dbHelper;

    public CommentDao(Context context) {
        this.dbHelper = new DatabaseHelper(context);
    }

    // 添加评论
    public long addComment(Comment comment) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_COMMENT_CONTENT, comment.getContent());
        values.put(DatabaseHelper.COLUMN_COMMENT_NEWS_ID, comment.getNewsId());
        values.put(DatabaseHelper.COLUMN_COMMENT_USER_NAME, comment.getUserName());

        long id = db.insert(DatabaseHelper.TABLE_COMMENT, null, values);
        db.close();
        return id;
    }

    // 删除评论
    public void deleteComment(int id) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(
                DatabaseHelper.TABLE_COMMENT,
                DatabaseHelper.COLUMN_ID + " = ?",
                new String[]{String.valueOf(id)}
        );
        db.close();
    }

    // 获取某个新闻的所有评论
    public List<Comment> getCommentsByNewsId(int newsId) {
        List<Comment> commentList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        
        String selectQuery = "SELECT * FROM " + DatabaseHelper.TABLE_COMMENT + 
                            " WHERE " + DatabaseHelper.COLUMN_COMMENT_NEWS_ID + " = ? " +
                            "ORDER BY " + DatabaseHelper.COLUMN_CREATE_TIME + " DESC";
        
        Cursor cursor = db.rawQuery(selectQuery, new String[]{String.valueOf(newsId)});

        if (cursor.moveToFirst()) {
            do {
                Comment comment = new Comment();
                comment.setId(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ID)));
                comment.setContent(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_COMMENT_CONTENT)));
                comment.setNewsId(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_COMMENT_NEWS_ID)));
                comment.setUserName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_COMMENT_USER_NAME)));
                comment.setCreateTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_CREATE_TIME)));

                commentList.add(comment);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return commentList;
    }

    // 获取某个新闻的评论数量
    public int getCommentCountByNewsId(int newsId) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        
        String countQuery = "SELECT COUNT(*) FROM " + DatabaseHelper.TABLE_COMMENT + 
                            " WHERE " + DatabaseHelper.COLUMN_COMMENT_NEWS_ID + " = ?";
        
        Cursor cursor = db.rawQuery(countQuery, new String[]{String.valueOf(newsId)});
        
        int count = 0;
        if (cursor != null && cursor.moveToFirst()) {
            count = cursor.getInt(0);
            cursor.close();
        }
        
        db.close();
        return count;
    }
} 